package com.zaizai.controller;

import com.zaizai.domain.AjaxResult;
import com.zaizai.domain.dto.CommodityDto;
import com.zaizai.service.IChartsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 购物车Controller
 */
@RestController
@RequestMapping("/carts")
public class CartsController {
    @Autowired
    private IChartsService chartsService;

    /** 获取购物列表 */
    @GetMapping("/list")
    public AjaxResult list() {
        List<CommodityDto> commodityDtoList = chartsService.selectCommodities();
        return AjaxResult.success(commodityDtoList);
    }

    /** 购物车中新增商品 */
    @PostMapping()
    public AjaxResult add(CommodityDto commodityDto) {
        chartsService.InsertCommodity(commodityDto);
        return AjaxResult.success();
    }

    /** 购物车中更新商品信息 */
    @PutMapping()
    public AjaxResult update(CommodityDto commodityDto) {
        chartsService.updateCommodity(commodityDto);
        return AjaxResult.success();
    }

    /** 购物车中删除商品 */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        chartsService.deleteCommodities(ids);
        return AjaxResult.success();
    }

    /** 购物车中购买商品 */
    @GetMapping("/purchase")
    public AjaxResult purchase() {
        chartsService.purchaseCharts();
        return AjaxResult.success();
    }
}
